import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class jdbc {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        int id;
        String name;
        System.out.println("请输入年龄：");
        id = scanner.nextInt();
        scanner.nextLine();
        System.out.println("请输入姓名：");
        name = scanner.nextLine();
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");
        Connection connection = dataSource.getConnection();
        /*String sql = "insert into student values(?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,id);
        preparedStatement.setString(2,name);
        int n = preparedStatement.executeUpdate();
        System.out.println("n = "+n);*/
        String sql = "select * from student";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            resultSet.getInt("id");
        }
        resultSet.close();
        preparedStatement.close();
        connection.close();
    }
}
